<template>
  <div v-show="show">
    <div>
      <a-table :columns="columns" rowKey="account" :data-source="scores" bordered :pagination="false"
               :scroll="{ y: 320 }">
        <template slot="title">
          <div style="text-align:center;font-size:18px">学生成绩</div>
        </template>
        <template slot="action" slot-scope="record">
          <a @click="correctError(record)">勘误</a>
        </template>
        <template slot="footer">
          共有{{ scores.length }}名学生选课,其中优秀率为：{{excellentNum/scores.length}}，合格率为{{passNum/scores.length}}：
        </template>
      </a-table>
    </div>
    <a-modal title="成绩勘误" @ok="handleOk" @cancel="handleCancel" :maskClosable="false" :visible="isCorrect"
             :confirm-loading="confirmLoading">
      <a-descriptions>
        <a-descriptions-item label="学生姓名">
          {{ currentStudent.studentName }}
        </a-descriptions-item>
        <a-descriptions-item label="学生学号">
          {{ currentStudent.account }}
        </a-descriptions-item>
        <a-descriptions-item label="学生原平时成绩">
          {{ currentStudent.usualScore }}
        </a-descriptions-item>
        <a-descriptions-item label="学生原考试成绩">
          {{ currentStudent.examScore }}
        </a-descriptions-item>
      </a-descriptions>
      <div>
        平时成绩：
        <a-input placeholder="勘误后学生平时成绩" allow-clear v-model="newUsualScore"/>
        <br/>
        <br/>
        考试成绩：
        <a-input placeholder="勘误后学生考试成绩" allow-clear v-model="newExamScore"/>
        勘误信息：
        <a-textarea placeholder="勘误信息" allow-clear v-model="description"/>
      </div>
    </a-modal>
  </div>
</template>

<script>
import getScores from "@/api/sessions/getStudentScores"
import {columns} from "@/pages/classInfo/js/studentColumns";
import errata from "@/api/selections/errata.js";

export default {
  name: "sessionScore",
  props: {
    courseItem: Object,
    show: Boolean
  },
  data() {
    return {
      excellentNum: 0,
      passNum: 0,
      course: this.courseItem,
      columns: [],
      scores: [],
      currentStudent: false,
      isCorrect: false,
      newUsualScore: '',
      newExamScore: '',
      isCorrectError: false,
      description: '',
      visible: false,
      confirmLoading: false
    }
  },
  mounted() {
    this.columns = columns.scoreColumns;
    this.getScores();
  },
  methods: {
    getScores() {
      let self = this;
      getScores(this.course.courseId).then(res => {
        if (res.result) {
          self.scores = res.scores;
          for (let i = 0; i < res.scores.length; i++) {
            if (res.scores[i] >= 85)
              self.excellentNum += 1;
            else if (res.scores[i] >= 60)
              self.passNum += 1;
          }
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    correctError(record) {
      this.currentStudent = record;
      this.isCorrect = true;
    },
    handleOk() {
      //先发送请求，如果成功再继续操作
      let self = this;
      if (!this.newUsualScore || !this.newExamScore || !this.description) {
        this.$message.error("勘误的成绩或信息不可为空");
      } else {
        this.confirmLoading = true;
        setTimeout(() => {
          this.confirmLoading = false;
        }, 2000);
        errata(self.course.courseId,
            self.currentStudent.account,
            self.description,
            self.currentStudent.usualScore,
            self.currentStudent.examScore,
            self.newUsualScore,
            self.newExamScore,
        ).then(res => {
          if (res.result == true) {
            self.isCorrect = false;
            self.newUsualScore = '';
            self.newExamScore = '';
            self.description = '';
          } else {
            this.$message.error(res.msg);
          }
        })
      }
    },
    handleCancel() {
      this.isCorrect = false;
      this.newUsualScore = '';
      this.newExamScore = '';
      this.description = '';
    }
  }
}
</script>

<style scoped>

</style>